﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebMovil.Base;

namespace WebMovil
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private string cliente = null;
        private string articulo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Tab1.CssClass = "Clicked";
                MainView.ActiveViewIndex = 0;
            }

            var bd = new DataClasses1DataContext();
            DropDownList1.DataSource = bd.SociosNegocios.Select(a => a.Nombre).ToArray();
            DropDownList1.DataBind();

            DropDownList2.DataSource = bd.MaestroArticulos.Select(b => b.Descripcion).ToArray();
            DropDownList2.DataBind();

        }

        protected void Tab1_Click(object sender, EventArgs e)
        {
            Tab1.CssClass = "Clicked";
            Tab2.CssClass = "Initial";
            Tab3.CssClass = "Initial";
            MainView.ActiveViewIndex = 0;
        }

        protected void Tab2_Click(object sender, EventArgs e)
        {
            Tab1.CssClass = "Initial";
            Tab2.CssClass = "Clicked";
            Tab3.CssClass = "Initial";
            MainView.ActiveViewIndex = 1;

            var bd = new DataClasses1DataContext();
            DropDownList4.DataSource = bd.SociosNegocios.Select(a => a.Nombre).ToArray();
            DropDownList4.DataBind();

            DropDownList6.DataSource = bd.MaestroArticulos.Select(b => b.Descripcion).ToArray();
            DropDownList6.DataBind();
        }

        protected void Tab3_Click(object sender, EventArgs e)
        {
            Tab1.CssClass = "Initial";
            Tab2.CssClass = "Initial";
            Tab3.CssClass = "Clicked";
            MainView.ActiveViewIndex = 2;
            var bd = new DataClasses1DataContext();

            DropDownList3.DataSource = bd.SociosNegocios.Select(a => a.Nombre).ToArray();
            DropDownList3.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.Visible = true;
            var bd = new DataClasses1DataContext();


            var articulosxcliente = from o in bd.SociosNegocios
                                    join a in bd.FacturaVentas on o.IdSocioNegocio equals a.IdSocioNegocio
                                    join p in bd.ArticulosXFacturaVentas on a.IdFacturaVentas equals p.IdFacturaVentas
                                    join r in bd.MaestroArticulos on p.IdArticulo equals r.IdMaestroArticulo
                                    where o.Nombre == DropDownList1.Text && r.Descripcion == DropDownList2.Text
                                    select new {r.Descripcion, p.Cantidad, p.PrecioVenta};

            GridView1.DataSource = articulosxcliente;
            GridView1.DataBind();
        }


        protected void Button3_Click(object sender, EventArgs e)
        {
            var bd = new DataClasses1DataContext();
            var limcredito = from p in bd.SociosNegocios where p.Nombre == DropDownList3.Text select p.LimiteCredito;
            var saldo = from v in bd.SociosNegocios where v.Nombre == DropDownList3.Text select v.Saldo;
            TextBox1.Text = limcredito.First().ToString();
            TextBox2.Text = saldo.First().ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var bd = new DataClasses1DataContext();


            var ventas = from o in bd.SociosNegocios
                         join a in bd.FacturaVentas on o.IdSocioNegocio equals a.IdSocioNegocio
                         //join p in bd.ArticulosXFacturaVentas on a.IdFacturaVentas equals p.IdFacturaVentas
                         where o.Nombre == DropDownList4.Text
                         select new {a.FechaContabilizacion.Date};

            var ventas2 = from o in bd.SociosNegocios
                         join a in bd.FacturaVentas on o.IdSocioNegocio equals a.IdSocioNegocio
                         //join p in bd.ArticulosXFacturaVentas on a.IdFacturaVentas equals p.IdFacturaVentas
                         where o.Nombre == DropDownList4.Text
                         select new { a.IdCompañia, a.FechaContabilizacion,a.Total};

            var maximo = ventas.First().Date.ToString();
            TextBox3.Text = maximo;
            ventas2 = ventas2.OrderByDescending(a => a.FechaContabilizacion).Take(12);
            var total = ventas2.Sum(a => a.Total);
            TextBox5.Text = total.ToString();

        }

    }
}